package com.chenglu.android.supermarket.fragment;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chenglu.android.R;
import com.chenglu.android.common.base.BaseMvpFragment;
import com.chenglu.android.common.net.Constant;
import com.chenglu.android.common.utils.JumpActivityUtils;
import com.chenglu.android.common.utils.LogUtils;
import com.chenglu.android.common.utils.StatusBarUtil;
import com.chenglu.android.goodsinfo.GoodsInfoActivity;
import com.chenglu.android.goodsinfo.infoutils.GoodsInfoUtils;
import com.chenglu.android.index.BannerUtils;
import com.chenglu.android.index.adapter.HomePicAdapter;
import com.chenglu.android.index.adapter.HomeTabTitleDataAdapter;
import com.chenglu.android.index.adapter.NearbyShopItemAdapter;
import com.chenglu.android.index.bean.BannerBeanList;
import com.chenglu.android.index.bean.IndexRecomBean;
import com.chenglu.android.index.bean.PageListBean;
import com.chenglu.android.restaurant.bean.RestaurantSearchBean;
import com.chenglu.android.search.SearchHistoryActivity;
import com.chenglu.android.supermarket.SuperMarkerActivity;
import com.chenglu.android.supermarket.adapter.SellGoodsAdapter;
import com.chenglu.android.supermarket.mvp.contract.SuperMarkerContract;
import com.chenglu.android.supermarket.mvp.presenter.SuperMarkerPresenter;
import com.youth.banner.Banner;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author: lyj
 * @Created: by Copyright 2023/9/22 Guangzhou Hongma Technology Co., Ltd. All rights reserved.
 * @Description：
 */
public class MarketHomeFragment extends BaseMvpFragment<SuperMarkerPresenter> implements SuperMarkerContract.View {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.rv_home_category)
    RecyclerView rvHomeCategory;
    @BindView(R.id.tv_item_todayall)
    TextView tvItemTodayall;
    @BindView(R.id.rv_sell_market)
    RecyclerView rvSellMarket;
    @BindView(R.id.ll_supermarket_sell)
    LinearLayout llSupermarketSell;
    @BindView(R.id.rv_super_like)
    RecyclerView rvSuperLike;
    @BindView(R.id.ic_back)
    ImageView icBack;
    @BindView(R.id.ll_back)
    LinearLayout llBack;
    @BindView(R.id.tv_search)
    TextView tvSearch;
    @BindView(R.id.ll_bgsearch)
    RelativeLayout llBgsearch;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.bannerlist)
    Banner bannerlist;


    //分类的adapter
    private HomePicAdapter homePicAdapter;

    private static SuperMarkerActivity activity;


    @Override
    protected int getLayoutId() {

        return R.layout.fragment_super_home_market;
    }

    @Override
    protected void initView() {
        tvTitle.setText("商超");
        StatusBarUtil.immersive(getActivity());
        StatusBarUtil.setPaddingSmart(getActivity(), toolbar);
        mPresenter = new SuperMarkerPresenter();
        mPresenter.attachView(this);

        initCategoryRv();

        initTitleDataAdapter();

        initHotsellRV();

        //图片分类
        mPresenter.getCategoryList(getMapdata());

        mPresenter.getBannerList(Constant.shopsTypeId999, Constant.one);

        //猜你喜欢
        mPresenter.getTabTitleData(setTabTitleMap(true, false,Constant.one));

        mPresenter.getHotsellData(setTabTitleMap(true,true,Constant.one));
        //


    }

    private SellGoodsAdapter sellGoodsAdapter;
    List<IndexRecomBean.Data.Records> sellGoodsList = new ArrayList<>();

    private void initHotsellRV() {

        rvSellMarket.setLayoutManager(new LinearLayoutManager(mContext,RecyclerView.HORIZONTAL,false));
        sellGoodsAdapter = new SellGoodsAdapter(mContext,sellGoodsList);
        rvSellMarket.setAdapter(sellGoodsAdapter);
        sellGoodsAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                GoodsInfoUtils.goodsInfo(mContext,sellGoodsAdapter.getData().get(position).getId(),1);
            }
        });
    }

    //图片分类的集合
    List<PageListBean.Data.Records> categoryList = new ArrayList<>();

    private void initCategoryRv() {

        homePicAdapter = new HomePicAdapter(mContext, categoryList);
        //
        rvHomeCategory.setLayoutManager(new GridLayoutManager(mContext, 4,
                LinearLayoutManager.VERTICAL, false));

        rvHomeCategory.setAdapter(homePicAdapter);

        homePicAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                PageListBean.Data.Records records = homePicAdapter.getData().get(position);
                JumpActivityUtils.searchResult(mContext, records.getCategoryName(), records.getShopsTypeId(),records.getId(),1);
            }
        });
    }

    List<IndexRecomBean.Data.Records> homeTitledataList = new ArrayList<>();
    private HomeTabTitleDataAdapter homeTabTitleDataAdapter;

    //推荐的商品列表 刷新和加载
    private void initTitleDataAdapter() {

        homeTabTitleDataAdapter = new HomeTabTitleDataAdapter(mContext, homeTitledataList);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext, 2);

        rvSuperLike.setLayoutManager(gridLayoutManager);
        rvSuperLike.setAdapter(homeTabTitleDataAdapter);

        homeTabTitleDataAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                GoodsInfoUtils.goodsInfo(mContext,homeTabTitleDataAdapter.getData().get(position).getId(),1);
            }
        });


    }

    private Map<String, String> getMapdata() {
        Map<String, String> map = new HashMap<>();
        map.put("isIdxShow", Constant.one);
        map.put("level", Constant.two);
        map.put("pid", activity.getPid);
        map.put("shopsTypeId", Constant.one);

        return map;
    }

    private Map<String, String> setTabTitleMap(boolean istype,boolean isRecommendPlatform, String shopsId) {
        Map<String, String> map = new HashMap<>();
        if (istype) {
            map.put("shopsTypeId", shopsId);
        }
        if(isRecommendPlatform){
            map.put("isHotPlatform", shopsId);
        }
        // 经纬度不授权 默认传0
        map.put("current", "1");
        map.put("size", 8 + "");
        return map;
    }

    public static MarketHomeFragment newInstance(SuperMarkerActivity superMarkerActivity) {
        MarketHomeFragment fragment = new MarketHomeFragment();
        activity = superMarkerActivity;
        return fragment;
    }


    @OnClick({R.id.ll_back, R.id.ll_bgsearch})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_back:
                getActivity().finish();
                break;
            case R.id.ll_bgsearch:
               mContext.startActivity(new Intent(getActivity(), SearchHistoryActivity.class)
               .putExtra(SearchHistoryActivity.SHOPSTYPEID,"1"));
                break;
        }
    }


    //图片分类
    @Override
    public void getCategoryList(PageListBean bean) {

        for (int i = 0; i < bean.getData().getRecords().size(); i++) {
            categoryList.add(bean.getData().getRecords().get(i));
        }
        homePicAdapter.notifyDataSetChanged();
    }


    @Override
    public void getBannerList(BannerBeanList bean) {

        List<String> imgs = new ArrayList<>();


        for (int i = 0; i < bean.getData().size(); i++) {
            imgs.add(bean.getData().get(i).getImg());
        }
        BannerUtils.initBanner(imgs, bannerlist);
    }


    @Override
    public void getTabTitleData(IndexRecomBean bean) {

        if (bean.getData().getRecords().size() > 0) {
            for (int i = 0; i < bean.getData().getRecords().size(); i++) {
                homeTitledataList.add(bean.getData().getRecords().get(i));
            }

        }
        homeTabTitleDataAdapter.notifyDataSetChanged();
    }



    @Override
    public void getHotsellData(IndexRecomBean bean) {

        if (bean.getData().getRecords().size() > 0) {
            for (int i = 0; i < bean.getData().getRecords().size(); i++) {
                sellGoodsList.add(bean.getData().getRecords().get(i));
            }

        }
        sellGoodsAdapter.notifyDataSetChanged();
    }

    @Override
    public void getSearchShop(RestaurantSearchBean bean) {

    }

}
